//
//  LBViewChangeAnimationDelegate.m
//  LittleBall
//
//  Created by apple on 11-11-20.
//  Copyright (c) 2011年 __MyCompanyName__. All rights reserved.
//

#import "LBViewChangeAnimationDelegate.h"
#import "LittleBallViewController.h"
#import "LittleBallConfigViewController.h"


#define LB_CONFIG_CTRLS_FIRST_LINE littleBallConfigViewController.m_lable_difficulty,littleBallConfigViewController.difficultySelection,littleBallConfigViewController.m_info

#define LB_CONFIG_CTRLS_SECOND_LINE littleBallConfigViewController.m_lable_music,littleBallConfigViewController.m_musicSlider

#define LB_CONFIG_CTRLS_THIRD_LINE littleBallConfigViewController.m_lable_sound,littleBallConfigViewController.m_soundSlider

#define LB_CONFIG_CTRLS_FOURTH_LINE littleBallConfigViewController.m_feedback,littleBallConfigViewController.m_confirm

#define LB_CONFIG_CTRLS_ALL LB_CONFIG_CTRLS_FIRST_LINE,LB_CONFIG_CTRLS_SECOND_LINE,LB_CONFIG_CTRLS_THIRD_LINE,LB_CONFIG_CTRLS_FOURTH_LINE

//////////////////////////////////////////////////////////////////////////////////////////////////////

#define LB_CONFIG_VIEWS_FIRST_LINE [[NSArray alloc] initWithObjects:LB_CONFIG_CTRLS_FIRST_LINE,nil];

#define LB_CONFIG_VIEWS_SECOND_LINE [[NSArray alloc] initWithObjects:LB_CONFIG_CTRLS_SECOND_LINE,nil];

#define LB_CONFIG_VIEWS_THIRD_LINE [[NSArray alloc] initWithObjects:LB_CONFIG_CTRLS_THIRD_LINE,nil];

#define LB_CONFIG_VIEWS_FOURTH_LINE [[NSArray alloc] initWithObjects:LB_CONFIG_CTRLS_FOURTH_LINE,nil];

#define LB_CONFIG_VIEWS_ALL [[NSArray alloc] initWithObjects:LB_CONFIG_CTRLS_ALL,nil];

@implementation LBViewChangeAnimationDelegate

@synthesize m_from;
@synthesize m_to;

-(id)initWithFrom:(id)from To:(id)to ChangeType:(LB_VIEW_CHANGE_TYPE)type
{
    //[[UIApplication sharedApplication] beginIgnoringInteractionEvents];
    
    if( !(self = [super init]))
    {
        return nil;
    }
    
    m_type = type;
    m_from = from;
    m_to = to;
    
    switch (m_type)
    {
    case LB_VIEW_CHANGE_MAIN_TO_CONFIG:
        {            
            LittleBallViewController *littleBallViewController = m_from;
            
            [self changeAlpha0To1OfView:littleBallViewController.m_traditionalMode Step:LB_VIEW_CHANGE_MAIN_APPEAR_STEP1];

        }
        break;
    
    case LB_VIEW_CHANGE_CONFIG_TO_MAIN:
        {            
            LittleBallConfigViewController *littleBallConfigViewController = m_from;
            
            NSArray *viewArray = LB_CONFIG_VIEWS_FIRST_LINE;
            
            [self changeAlpha0To1OfViewArrary:viewArray Step:LB_VIEW_CHANGE_CONFIG_DISAPPEAR_STEP1];
        }
        break;
            
    case LB_VIEW_CHANGE_CONFIG_TO_INFO:
        {
            LittleBallConfigViewController *littleBallConfigViewController = m_from;
            UIView *infoView = m_to;
            
            infoView.hidden = true;
            infoView.center = CGPointMake(384.0f, 350.0f);
            
            NSArray *viewArray = LB_CONFIG_VIEWS_ALL;
            
            [self changeAlpha0To1OfViewArrary:viewArray Step:LB_VIEW_CHANGE_CONFIG_DISAPPEAR_ALL];
        }
        break;
            
        case LB_VIEW_CHANGE_INFO_TO_CONFIG:
        {
            //LittleBallConfigViewController *littleBallConfigViewController = m_from;
            UIView *infoView = m_from;

            [self changeAlpha0To1OfView:infoView Step:LB_VIEW_CHANGE_INFO_DISAPPEAR_ALL];
            
        }
        break;
            
    default:
        break;
    }
    
    return self;
}

- (void)animationWillStart:(NSString *)animationID context:(void *)context
{
    // do nothing...    
}

- (void)animationDidStop:(NSString *)animationID finished: (NSNumber *)finished context:(void *)context
{    
    switch (m_type) 
    {
    // 从主菜单切换到游戏设置界面
    case LB_VIEW_CHANGE_MAIN_TO_CONFIG:
        {
            LittleBallViewController *littleBallViewController = m_from;
            LittleBallConfigViewController *littleBallConfigViewController = m_to;
            
            switch (m_step) 
            {
            case LB_VIEW_CHANGE_MAIN_APPEAR_STEP1:
                {
                    littleBallViewController.m_traditionalMode.hidden = true;
                    [self changeAlpha0To1OfView:littleBallViewController.m_timeMode Step:LB_VIEW_CHANGE_MAIN_APPEAR_STEP2];
                }
                break;
                    
            case LB_VIEW_CHANGE_MAIN_APPEAR_STEP2:
                {
                    littleBallViewController.m_timeMode.hidden = true;
                    [self changeAlpha0To1OfView:littleBallViewController.m_gameConfig Step:LB_VIEW_CHANGE_MAIN_APPEAR_STEP3];
                }
                break;
                    
            case LB_VIEW_CHANGE_MAIN_APPEAR_STEP3:
                {
                    littleBallViewController.m_gameConfig.hidden = true;
                    [self changeAlpha0To1OfView:littleBallViewController.m_gameExit Step:LB_VIEW_CHANGE_MAIN_APPEAR_STEP4];
                }
                break;
                
            case LB_VIEW_CHANGE_MAIN_APPEAR_STEP4:
                {
                    littleBallViewController.m_gameExit.hidden = true;
                    int viewWidth = littleBallConfigViewController.view.bounds.size.width;
                    int viewHeight = littleBallConfigViewController.view.bounds.size.height;
                    
                    littleBallConfigViewController.view.center = CGPointMake(viewWidth/2, viewHeight/2);
                    [littleBallViewController.view addSubview:littleBallConfigViewController.view];
                    littleBallConfigViewController.creator = littleBallViewController;
                    
                    /*
                    littleBallConfigViewController.difficultySelection.hidden = true;
                    littleBallConfigViewController.m_info.hidden = true;
                    littleBallConfigViewController.m_musicSlider.hidden = true;
                    littleBallConfigViewController.m_feedback.hidden = true;
                    littleBallConfigViewController.m_soundSlider.hidden = true;
                    littleBallConfigViewController.m_cancel.hidden = true;
                    littleBallConfigViewController.m_confirm.hidden = true;
                     */
                    NSArray *viewArray = LB_CONFIG_VIEWS_FOURTH_LINE;
                    [self setProptsOfViews:viewArray Hidden:true];
                    
                    viewArray = LB_CONFIG_VIEWS_THIRD_LINE;
                    [self setProptsOfViews:viewArray Hidden:true];
                    
                    viewArray = LB_CONFIG_VIEWS_SECOND_LINE;
                    [self setProptsOfViews:viewArray Hidden:true];
                    
                    viewArray = LB_CONFIG_VIEWS_FIRST_LINE;
                    [self setProptsOfViews:viewArray Hidden:true];
                    
                    [self changeAlpha1To0OfViewArrary:viewArray Step:LB_VIEW_CHANGE_CONFIG_APPEAR_STEP1];
                }
                break;
                
            case LB_VIEW_CHANGE_CONFIG_APPEAR_STEP1:
                {
                    NSArray *viewArray = LB_CONFIG_VIEWS_SECOND_LINE;
                    
                    [self changeAlpha1To0OfViewArrary:viewArray Step:LB_VIEW_CHANGE_CONFIG_APPEAR_STEP2];
                }
                break;
                
            case LB_VIEW_CHANGE_CONFIG_APPEAR_STEP2:
                {
                    NSArray *viewArray = LB_CONFIG_VIEWS_THIRD_LINE;
                    
                    [self changeAlpha1To0OfViewArrary:viewArray Step:LB_VIEW_CHANGE_CONFIG_APPEAR_STEP3];
                }
                break;
                
            case LB_VIEW_CHANGE_CONFIG_APPEAR_STEP3:
                {
                    NSArray *viewArray = LB_CONFIG_VIEWS_FOURTH_LINE;
                    
                    [self changeAlpha1To0OfViewArrary:viewArray Step:LB_VIEW_CHANGE_CONFIG_APPEAR_STEP4];
                }
                break;
                
            case LB_VIEW_CHANGE_CONFIG_APPEAR_STEP4:
                {
                    // nothing to do...
                }
                break;
                
            default:
                break;
            }
        }
        break;
    
    // 从游戏设置界面切换到主菜单
    case LB_VIEW_CHANGE_CONFIG_TO_MAIN:
        {
            LittleBallViewController *littleBallViewController = m_to;
            LittleBallConfigViewController *littleBallConfigViewController = m_from;
            
            switch (m_step) 
            {
            case LB_VIEW_CHANGE_CONFIG_DISAPPEAR_STEP1:
                {
                    NSArray *viewArray = LB_CONFIG_VIEWS_FIRST_LINE;
                    [self setProptsOfViews:viewArray Hidden:true];
                    
                    viewArray = LB_CONFIG_VIEWS_SECOND_LINE;
                    
                    [self changeAlpha0To1OfViewArrary:viewArray Step:LB_VIEW_CHANGE_CONFIG_DISAPPEAR_STEP2];
                }
                break;
                
            case LB_VIEW_CHANGE_CONFIG_DISAPPEAR_STEP2:
                {
                    NSArray *viewArray = LB_CONFIG_VIEWS_SECOND_LINE;
                    [self setProptsOfViews:viewArray Hidden:true];
                    
                    viewArray = LB_CONFIG_VIEWS_THIRD_LINE;
                    
                    [self changeAlpha0To1OfViewArrary:viewArray Step:LB_VIEW_CHANGE_CONFIG_DISAPPEAR_STEP3];
                }
                break;
                
            case LB_VIEW_CHANGE_CONFIG_DISAPPEAR_STEP3:
                {
                    NSArray *viewArray = LB_CONFIG_VIEWS_THIRD_LINE;
                    [self setProptsOfViews:viewArray Hidden:true];
                    
                    viewArray = LB_CONFIG_VIEWS_FOURTH_LINE;
                    
                    [self changeAlpha0To1OfViewArrary:viewArray Step:LB_VIEW_CHANGE_CONFIG_DISAPPEAR_STEP4];
                }
                break;
                
            case LB_VIEW_CHANGE_CONFIG_DISAPPEAR_STEP4:
                {                    
                    NSArray *viewArray = LB_CONFIG_VIEWS_FOURTH_LINE;
                    [self setProptsOfViews:viewArray Hidden:true];
                    
                    [littleBallConfigViewController.view removeFromSuperview];
                    
                    [self changeAlpha1To0OfView:littleBallViewController.m_traditionalMode Step:LB_VIEW_CHANGE_MAIN_APPEAR_STEP1];
                }
                break;
                
            case LB_VIEW_CHANGE_MAIN_APPEAR_STEP1:
                {
                    [self changeAlpha1To0OfView:littleBallViewController.m_timeMode Step:LB_VIEW_CHANGE_MAIN_APPEAR_STEP2];
                }
                break;
                
            case LB_VIEW_CHANGE_MAIN_APPEAR_STEP2:
                {
                    [self changeAlpha1To0OfView:littleBallViewController.m_gameConfig Step:LB_VIEW_CHANGE_MAIN_APPEAR_STEP3];
                }
                break;

                
            case LB_VIEW_CHANGE_MAIN_APPEAR_STEP3:
                {
                    [self changeAlpha1To0OfView:littleBallViewController.m_gameExit Step:LB_VIEW_CHANGE_MAIN_APPEAR_STEP4];
                }
                break;

                
            case LB_VIEW_CHANGE_MAIN_APPEAR_STEP4:
                {
                    // nothing to do...
                }
                break;

                    
            default:
                break;
            }
        }
        break;
            
    case LB_VIEW_CHANGE_CONFIG_TO_INFO:
        {
            LittleBallConfigViewController *littleBallConfigViewController = m_from;
            UIView *infoView = m_to;
            
            switch (m_step) 
            {
            case LB_VIEW_CHANGE_CONFIG_DISAPPEAR_ALL:
                {
                    NSArray *viewArray = LB_CONFIG_VIEWS_ALL;
                    [self setProptsOfViews:viewArray Hidden:true];
                    
                    [self changeAlpha1To0OfView:infoView Step:LB_VIEW_CHANGE_INFO_APPEAR_ALL];
                    
                }
                break;
                    
            case LB_VIEW_CHANGE_INFO_APPEAR_ALL:
                {
                    
                }
                break;
                
            default:
                break;
            }
        
            
        }
        break;
            
    case LB_VIEW_CHANGE_INFO_TO_CONFIG:
        {
            LittleBallConfigViewController *littleBallConfigViewController = m_to;
            UIView *infoView = m_from;
            
            switch (m_step) 
            {
            case LB_VIEW_CHANGE_INFO_DISAPPEAR_ALL:
                {
                    [infoView removeFromSuperview];
                    
                    NSArray *viewArray = LB_CONFIG_VIEWS_ALL;
                    
                    [self changeAlpha1To0OfViewArrary:viewArray Step:LB_VIEW_CHANGE_CONFIG_APPEAR_ALL];
                    
                }
                break;
                
            case LB_VIEW_CHANGE_CONFIG_APPEAR_ALL:
                {
                    
                }
                break;
                
            default:
                break;
            }
        }
            break;
            
    default:
        break;
    }
}


// 从不透明变为透明
- (void)changeAlpha0To1OfView:(UIView *)view Step:(LB_VIEW_CHANGE_STEP)step
{
    [view setAlpha:cosf(LB_ALPHA_0)];
    
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationRepeatCount:1];
    [UIView setAnimationDuration:ALPHA_CHANGE_SPEED];
    [view setAlpha:cosf(LB_ALPHA_1)];
    [UIView commitAnimations];
    
    m_step = step;
}

// 从透明变为不透明
- (void)changeAlpha1To0OfView:(UIView *)view Step:(LB_VIEW_CHANGE_STEP)step
{
    [view setAlpha:cosf(LB_ALPHA_1)];
    view.hidden = false;
    
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationRepeatCount:1];
    [UIView setAnimationDuration:ALPHA_CHANGE_SPEED];
    [view setAlpha:cosf(LB_ALPHA_0)];
    [UIView commitAnimations];
    
    m_step = step;
}

// 从不透明变为透明
- (void)changeAlpha0To1OfViewArrary:(NSArray *)viewArray Step:(LB_VIEW_CHANGE_STEP)step
{
    for (int i=0; i < [viewArray count]; i++) 
    {
        NSLog(@"array length: %d, objectAtIndex:%d, File:%s, Line:%d",[viewArray count],i,__FILE__,__LINE__);
        UIView *view = [viewArray objectAtIndex:i];
        [view setAlpha:cosf(LB_ALPHA_0)];
    }
    
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationRepeatCount:1];
    [UIView setAnimationDuration:ALPHA_CHANGE_SPEED];
    
    for (int i=0; i < [viewArray count]; i++) 
    {
        NSLog(@"array length: %d, objectAtIndex:%d, File:%s, Line:%d",[viewArray count],i,__FILE__,__LINE__);
        UIView *view = [viewArray objectAtIndex:i];
        [view setAlpha:cosf(LB_ALPHA_1)];
    }
    
    [UIView commitAnimations];
    
    m_step = step;
}

// 从透明变为不透明
- (void)changeAlpha1To0OfViewArrary:(NSArray *)viewArray Step:(LB_VIEW_CHANGE_STEP)step
{
    for (int i=0; i < [viewArray count]; i++) 
    {
        NSLog(@"array length: %d, objectAtIndex:%d, File:%s, Line:%d",[viewArray count],i,__FILE__,__LINE__);
        UIView *view = [viewArray objectAtIndex:i];
        [view setAlpha:cosf(LB_ALPHA_1)];
        view.hidden = false;
    }
    
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationRepeatCount:1];
    [UIView setAnimationDuration:ALPHA_CHANGE_SPEED];
    
    for (int i=0; i < [viewArray count]; i++) 
    {
        NSLog(@"array length: %d, objectAtIndex:%d, File:%s, Line:%d",[viewArray count],i,__FILE__,__LINE__);
        UIView *view = [viewArray objectAtIndex:i];
        [view setAlpha:cosf(LB_ALPHA_0)];
    }
    
    [UIView commitAnimations];
    
    m_step = step;
}

// 设置 view 数组的 hidden 属性
- (void)setProptsOfViews:(NSArray *)viewArray Hidden:(BOOL)bHidden
{
    for (int i=0; i < [viewArray count]; i++) 
    {
        NSLog(@"array length: %d, objectAtIndex:%d, File:%s, Line:%d",[viewArray count],i,__FILE__,__LINE__);
        UIView *view = [viewArray objectAtIndex:i];
        view.hidden = bHidden;
    }
}
@end